﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAL.DataOperate
{
    /// <summary>
    /// 实现分页
    /// </summary>
    public class PageDivided:DAL.App_Code.DBBase
    {
        public PageDivided() : base() { }
        /// <summary>
        /// 实现不同表的分页
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="FdOrder">排序的列（一般是自增编号列）</param>
        /// <param name="where">筛选条件</param>
        /// <param name="pageSize">页面大小（每页显示的记录书）</param>
        /// <param name="pageIndex">页面索引（第几页）</param>
        /// <returns>DataSet数据集</returns>
        public DataSet Select(String table, String FdOrder,String where, int pageSize, int pageIndex)
        {
            try
            {
                if ((table == "" || table == null) ||
                    (pageSize.ToString() == "" || pageSize.ToString() == null) ||
                    (pageIndex.ToString() == "" || pageIndex.ToString() == null))
                {
                    return null;
                }
                SqlParameter[] pa = {
                new SqlParameter("@table",table),
                new SqlParameter("@FdOrder",FdOrder),
                new SqlParameter("@where",where),
                new SqlParameter("@pageSize",pageSize),
                new SqlParameter("@pageIndex",pageIndex)
                                };

                return ExecuteProcedure("pr_PageDivided", pa, table);
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 获得table 的所有记录数目
        /// </summary>
        /// <param name="table"></param>
        /// <param name="where">筛选条件</param>
        /// <returns></returns>
        public int GetTotalRow(String table, String where)
        {
            if (table == "" || table == null)
            {
                return -1;
            }
            else
            {
                try
                {
                    StringBuilder sqlStr = new StringBuilder();
                    DataSet ds = new DataSet();
                    int total = 0;

                    if (where == "" || where == null)
                        where = " 1 = 1 ";   // 无条件
                    sqlStr.Append("select count(*) as co from ");
                    sqlStr.Append(table + " ");
                    sqlStr.Append(" where ");
                    sqlStr.Append(where);

                    SqlParameter[] pa = { };
                    ds = ExecuteString(sqlStr.ToString(), pa, "News");

                    if ((ds == null) ||
                       (ds.Tables.Count == 0) ||
                       (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
                    {
                        return -1;
                    }
                    else
                    {
                        total = Int32.Parse(ds.Tables["News"].Rows[0]["co"].ToString());
                        return total;
                    }
                }
                catch
                {
                    return -1;
                }
            }
        }
    }
}
